package com.yidu.hbatsams.service.Impl;

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsams.dao.AtsEmpExpertMapper;
import com.yidu.hbatsams.dao.AtsSchemeAuditMapperfx;
import com.yidu.hbatsams.dao.AtsSchemeExpertMapperfx;
import com.yidu.hbatsams.domain.AtsEmpExpert;
import com.yidu.hbatsams.domain.AtsSchemeAudit;
import com.yidu.hbatsams.domain.AtsSchemeExpert;
import com.yidu.hbatsams.service.AtsSchemeAuditServicefx;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
@Service
public class AtsSchemeAuditImplfx implements AtsSchemeAuditServicefx {
    @Resource
    private AtsSchemeAuditMapperfx atsSchemeAuditMapperfx;
    @Resource
    private AtsEmpExpertMapper atsEmpExpertMapper;
    @Resource
    private AtsSchemeExpertMapperfx atsSchemeExpertMapperfx;
    @Override
    public List<AtsSchemeAudit> list(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageSize,pageNum);
        List<AtsSchemeAudit> lis = atsSchemeAuditMapperfx.list();
        return lis;
    }

    @Override
    public int count(String schemeType) {
        return atsSchemeAuditMapperfx.count(schemeType);
    }

    @Override
    public List<AtsEmpExpert> selectAll(String expertName) {
       /*List<AtsEmpExpert> list = atsEmpExpertMapper.selectAll(expertName);
        return list;*/
       return  null;
    }

    @Override
    public int deleteByPrimaryKey(Integer auId) {
        return 0;
    }

    @Override
    public int insert(AtsSchemeAudit record) {
        return 0;
    }

    @Override
    public int insertSelective(AtsSchemeAudit record) {
        return 0;
    }

    @Override
    public List<AtsSchemeAudit> selectByPrimaryKey(Integer pageSize,Integer pageNum,String schemeType) {
        PageHelper.startPage(pageNum,pageSize);
        List<AtsSchemeAudit> lis = atsSchemeAuditMapperfx.selectByPrimaryKey(schemeType);
        System.out.println(pageSize+"数量");
        return lis;
    }

    @Override
    public int updateByPrimaryKeySelective(AtsSchemeAudit record, AtsSchemeExpert expert,String saId) {
        System.out.println(saId);
        AtsSchemeAudit audit = atsSchemeAuditMapperfx.findById(record.getSaId());
        System.out.println("record.getSaId()"+record.getSaId()+"service");
        //获取当前时间
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        audit.setAuTime(dateFormat.format(calendar.getTime()));

        audit.setAuExp(record.getAuExp());
        audit.setExpertId(record.getExpertId());
        audit.setExpertName(record.getExpertName());
        audit.setAuditState("领导已同意");
        audit.setSort(20);
        if(audit.getSaId()!=null){
            AtsSchemeExpert schemeExpert = atsSchemeExpertMapperfx.findById(Integer.parseInt(audit.getSaId()));
            System.out.println(schemeExpert.getSchemeId());
            schemeExpert.setIsva(1);
            schemeExpert.setAuditState("领导已同意");

            AtsEmpExpert empExpert = atsEmpExpertMapper.selectByPrimaryKey(record.getExpertId());
            schemeExpert.setExpertId(empExpert.getExpertId());
            schemeExpert.setExpertName(empExpert.getExpertName());
            schemeExpert.setSort(20);
            atsSchemeExpertMapperfx.updateByPrimaryKeySelective(schemeExpert);
        }
        return atsSchemeAuditMapperfx.updateByPrimaryKeySelective(audit);
    }

    @Override
    public AtsSchemeAudit findById(String saId) {
        AtsSchemeAudit audit = atsSchemeAuditMapperfx.findById(saId);
        return audit;
    }

    @Override
    public int updateByPrimaryKey(AtsSchemeAudit record) {
        AtsSchemeAudit audit = atsSchemeAuditMapperfx.findById(record.getSaId());
        //获取当前时间

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        audit.setAuTime(dateFormat.format(calendar.getTime()));

        audit.setAuExp(record.getAuExp());
        audit.setAuditState("领导不同意");
        audit.setSort(21);
        if(audit.getSaId()!=null){
            AtsSchemeExpert schemeExpert = atsSchemeExpertMapperfx.findById(Integer.parseInt(audit.getSaId()));
            schemeExpert.setAuditState("领导不同意");
            schemeExpert.setSort(21);
            atsSchemeExpertMapperfx.updateByPrimaryKeySelective(schemeExpert);
        }
        return atsSchemeAuditMapperfx.updateByPrimaryKeySelective(audit);
    }


}
